﻿using UnityEngine;
using System.Collections;
using System;
using SubPhysicsColliderInfo = System.Tuple<uint, BelongingSystem, ElementState>;
using ScFramework.Core;
using System.Collections.Generic;

public partial class FuncManager : IFuncAPI
{
    public void StartCombine(SCore core)
    {
        Vector3 pos = core.transform.position;
        var body = core.Entity.GetComponent<SComBody>();
        Vector3Int meshpos =body.MeshPos;
        Vector2 boxSiz = Vector2.one * core.CombineMeshRange * Main.ObejectBlockSize;
        Debug.Assert(body.UserID != 0);
        Debug.Assert(GetInnerModule<IBelongUser>().GetBelongUser(body.EntityId) == body.UserID);
        int tp = core.CombineMeshRange / 2;

        Vector3 centerPos = pos +(Vector3)((Vector3Int)(Vector2Int.one*tp)-meshpos)* Main.ObejectBlockSize;

        List<Vector3> posList = new List<Vector3>();
        for(int i = -core.DetectMeshRange; i <= core.DetectMeshRange; i++)
        {
            for(int j = -core.DetectMeshRange; j <= core.DetectMeshRange; j++)
            {
                var npos = centerPos + new Vector3(i, j, 0) * Main.ObejectBlockSize;
  
                var list = GetInnerModule<ISharedCollisionManager>().GetOverlapBox(npos, boxSiz,0,core.gameObject.layer);//GetAllCollidersInfoInBox(npos,boxSiz,core.gameObject.layer);
                list.RemoveAll(s => body.UserID == GetInnerModule<IBelongUser>().GetBelongUser(s.Item2.left()));
                if (list.Count == 0)
                {
                    posList.Add(npos);
                }
            }
        }
        if (posList.Count == 0)
        {
            UIManager.Instance.Open("DialogWindowUI", 5, args: new DialogWindowUI.DialogWindowUIArg
            ("注意", "周围有障碍", null, "确定"));
        }
        else
        {
            posList.Sort((Vector3 a, Vector3 b) =>
            {
                if (Vector3.Distance(a, centerPos) < Vector3.Distance(b, centerPos)) return - 1;
                else return 1;
            });
            var entitis = GetInnerModule<IBelongUser>().GetAllEntitiesByUserID(body.UserID).ConvertAll(s=>GetInnerModule<IEntityMgrInner>().GetEntity(s));
            entitis.RemoveAll(s => s.GetComponent<SRigidUnion>() != null);
            Dispatch(PhysicsProcessEvent.StartCombine,new Tuple<List<Entity>,Vector3>(entitis,posList[0]));
        }


    }
    public List<SubPhysicsColliderInfo> GetAllCollidersInfoInBox(Vector2 pos,Vector2 siz,LayerMask layer)
    {
        
       var colliders= Physics2D.OverlapBoxAll(pos,siz, 0f,layer);
        List<SubPhysicsColliderInfo> ans = new List<SubPhysicsColliderInfo>();
        foreach(var x in colliders)
        {
            if (x.gameObject.GetComponent<ISubPhysicsCollider>() != null)
            {
                ans.Add(x.gameObject.GetComponent<ISubPhysicsCollider>().GetObjectInfo());
            }
        }
        return ans;
    }

    public Tuple<BelongingSystem, uint> Create(BelongingSystem sys,ElementState state, Vector2 pos)
    {
        Debug.Assert(sys != BelongingSystem.Default);
        var id = GetInnerModule<ISharedCollisionManager>().Create(sys, state, pos);
        if(id == 0)
        {
            return null;
        }

        return new Tuple<BelongingSystem, uint>(sys, GetInnerModule<ISharedCollisionManager>().Create(sys, state, pos));
    }
}
